import SwiftUI
import CryptoKit
import Zip
fileprivate let key = "iScreen.ThemeDiy.FileName.Key1"

struct ContentView: View {
    @State var currentIndex: Int = 0
    var base: Double = 0.9
    @State var list: [Data] = []
    var body: some View {
        GeometryReader { geo in
            ZStack{
                Color.red
                VStack(spacing: 20) {
                    Button("create") {
                        createAction()
                    }.padding()
                    Button("zip") {
                        zipAction()
                    }.padding()
                    Button("unzip") {
                        unZip()
                    }.padding()
                    Button("writeAction") {
                        writeAction()
                      //  write()
  
                    }.padding()
                    Button("readAction") {
                        readTxt()
  
                    }.padding()
                }
            }
        }.ignoresSafeArea().onAppear {


        }
    }
    func writeAction(){
        var list: [Data] = []
       if let data1 = try? "11".data(using: .utf8){
           list.append(data1)
       }
       if let data1 = try? "adfas".data(using: .utf8){
           list.append(data1)
       }
       if let data1 = try? "3adfas3".data(using: .utf8){
           list.append(data1)
       }
       if let data = try? JSONEncoder().encode(list){
           
           let url = URL(fileURLWithPath: basePath)
           let fileURL = url.appendingPathComponent("data111.txt")
           

//           if let data1 = try? "33".data(using: .utf8){
//               try? data1.write(to: fileURL,options: .atomic)
//           }
           do{
               try data.write(to: fileURL, options: .atomic)

           }catch{
               debugPrint(error)
           }
       }
    
    }
    let document = NSHomeDirectory() + "/Documents"
    let basePath = NSHomeDirectory() + "/Documents/Caches"
    let filePath = NSHomeDirectory() + "/Documents/Caches/Sqlite"
    let filePathtxt = NSHomeDirectory() + "/Documents/Caches/data111.txt"
    func createAction(){
        let file = FileManager.default
        debugPrint(filePath)
        if file.fileExists(atPath: filePath) == false{
            if let _ = try? file.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil){
                debugPrint("create path success")
            }
            
        }
         
        let image = UIImage(named: "bg100")
        let data = image?.jpegData(compressionQuality: 1) ?? Data()
        let path = filePath + "/" + "bg100.jpg"
        if let _ =  try? data.write(to: URL(fileURLWithPath: path), options: .atomic){
            debugPrint("图片成功")
        }
        
    }
    func zipAction(){
        let url = URL(fileURLWithPath: basePath)
        if let urlPath = try? Zip.quickZipFiles([url], fileName: "tianya", progress: { progress in
            debugPrint(progress)
        }){
            debugPrint(urlPath)
            if let data = try? Data(contentsOf: urlPath){
                debugPrint(data.count)
            }
        }
        let file = FileManager.default
        
//        if let _ = try? file.removeItem(at: url){
//            
//        }
    }
    func unZip(){
        let url = URL(fileURLWithPath: document + "/tianya.zip")
        do{
            debugPrint(url)
            let url1 = try Zip.quickUnzipFile(url) { progress in
                
            }
        }catch{
            debugPrint(error)
        }
    }
    func deleteAction(){
        let file = FileManager.default
        let url = URL(fileURLWithPath: document + "/tianya.zip")
        if let _ = try? file.removeItem(at: url){
            
        }
    }
    func readTxt(){
        if let data = try? Data(contentsOf: URL(fileURLWithPath: filePathtxt)){
            if let list = try? JSONDecoder().decode([Data].self, from: data){
                list.forEach { objc in
                    let message = String(data: objc, encoding: .utf8)
                    debugPrint(message)
                }
            }
        }
    }
}
